# -*- coding: UTF-8 -*-
import mysql.connector as db
import client.tWeibo
import time
import json
import re
import sys

def get_account(html):
	group = re.search('<li data-uin="[^"]+" account="(?P<account>[^"]+)">', html, re.I)
	if group is None:
		return None
	return group.groupdict()['account']
def get_nickname(html):
	group = re.search('target="_blank" title="(?P<nickname>[^\(]+)\(@[^"]+\)" class="fb">', html, re.I)
	if group is not None:
		return group.groupdict()['nickname']
	group = re.search('target="_blank" title="[^"]*" class="fb" >(?P<nickname>.+?)</a>', html, re.I)
	if group is not None:
		return group.groupdict()['nickname']
	return None
def get_certification(html):
	group = re.search('<strong><a .+?</a><a href="[^"]+" title="(?P<certification>[^"]+)" target="_blank" class="[^"]+"></a><span><i .+?</strong>', html)
	if group is None:
		return None
	return group.groupdict()['certification']
def get_detail(html):
	group = re.search('<p class="info_item">(?P<detail>.+?)</p>', html, re.I)
	if group is None:
		return None
	return group.groupdict()['detail'].split('|')
def make_insert_sql(table, data):
	sql = "INSERT IGNORE INTO `microFriends` SET " + ' ,'.join([
	"`account`='%s'" % data['account'],
	"`nick_name`='%s'" % data['nick_name'],
	"`age`=%d" %int(data['age'].replace('岁','')),
	"`address`='%s'" % data['address'],
	"`height`=%d" % int(data['height'].replace('厘米','')),
	"`education`='%s'" % data['education'],
	"`gender`='%s'" % data['gender'],
	"`certification`='%s'" % data['certification'],
	"`job`='%s';" % data['job'],])
	# print(sql)
	return sql
def parse_info(infos):
	sqls = []
	detail = ['age', 'address', 'height', 'education', 'job']
	for info in infos:
		data = {
			'account':get_account(info), 
			'nick_name':get_nickname(info), 
			'certification':get_certification(info),
			'gender':'男', 
			'age':'0', 'address':'', 'height':'0', 'education':'', 'job':'',
			}
		if '<i class="ico_female"></i>' in  info:
			data['gender'] = '女'
		info_item = get_detail(info)
		for i in range(len(info_item)):
			data[detail[i]] = info_item[i].strip()
		sqls.append(make_insert_sql('microfriends', data))
	return sqls
if __name__ == '__main__':
	connect = db.connect(user='root',db='collection',password='',host="127.0.0.1")
	cursor = connect.cursor()
	cursor.execute("SET SQL_MODE = 'TRADITIONAL'")
	
	uin = 'QQ号'
	passwd = '密码'
	wb = client.tWeibo.tWeibo(uin, passwd)
	wb.login()
	provinces = [('北京','11'),('天津','12'),('上海','31'),('河北','13'),('山西','14'),('内蒙古','15'),('辽宁','21'),('吉林','22'),('黑龙江','23'),('江苏','32'),('浙江','33'),('安徽','34'),('福建','35'),('江西','36'),('山东','37'),('河南','41'),('湖北','42'),('湖南','43'),('广东','44'),('广西','45'),('海南','46'),('重庆','50'),('四川','51'),('贵州','52'),('云南','53'),('西藏','54'),('陕西','61'),('甘肃','62'),('青海','63'),('宁夏','64'),('新疆','65'),('台湾','71'),('香港','81'),('澳门','82'),]
	for (province, provinceId) in provinces:
		html = wb.get('http://zhaoren.t.qq.com/microFriends.php?sex=0&age=0&height=0&degree=0&nation_view=1&province_view=~PROVINCE~&city_view='.replace('~PROVINCE~', provinceId)).data.decode('utf-8')
		group = re.search('<ul class="clear">(?P<infos>.+?)</ul>', html, re.S)
		if group is None:
			print('没有获取到列表')
			sys.exit()
		# re.sub('>\s+<', '',group.groupdict()['infos'].replace("\r\n",'')).strip().replace('</li><li','</li>\r\n<li')
		infos = re.sub('>\s+<', '><',group.groupdict()['infos'].replace("\r\n",'')).strip().replace('</li><li','</li>\r\n<li').split('\r\n')
		page = 1
		fp = open(province + provinceId + '.txt', 'w', encoding='utf-8')
		while True:
			time.sleep(6)
			for sql in parse_info(infos):
				cursor.execute(sql)
			connect.commit()
			rs = wb.microFriend(page * 12, provinceId)
			# print(page, rs.strip())
			print('%s - %s - %d' % (province, provinceId, page))
			if '{"result":0,"info":""' in rs :
				break
			fp.write(rs + '\n')
			infos = json.loads(rs)['info'].replace('</li><li','</li>\r\n<li').split('\r\n')
			page += 1
		fp.close()
	connect.commit()
	cursor.close()
	connect.close()
